package com.gewu.pm.ui.adapter.sales;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;
import com.gewu.pm.bean.TerminalContactBean;

/**
 * 终端联系人列表适配器
 */
public class TerminalContactAdapter extends AppAdapter<TerminalContactBean> {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_ADD = 1;

    private OnContactActionListener mListener;

    public TerminalContactAdapter(Context context) {
        super(context);
    }

    /**
     * 设置操作监听器
     */
    public void setOnContactActionListener(OnContactActionListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemCount() {
        // 加1是因为底部有新增按钮
        return super.getItemCount() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == getCount()) {
            return TYPE_ADD;
        }
        return TYPE_ITEM;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ADD) {
            return new AddViewHolder();
        } else {
            return new ItemViewHolder();
        }
    }

    /**
     * 联系人 Item ViewHolder
     */
    private final class ItemViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mNameView;
        private final TextView mPhoneView;
        private final TextView mNoteView;
        private final TextView mPrimaryTag;
        private final TextView mEditButton;
        private final android.widget.ImageView mDeleteButton;

        private ItemViewHolder() {
            super(R.layout.terminal_contact_item);
            mNameView = findViewById(R.id.tv_contact_name);
            mPhoneView = findViewById(R.id.tv_contact_phone);
            mNoteView = findViewById(R.id.tv_contact_note);
            mPrimaryTag = findViewById(R.id.tv_primary_tag);
            mEditButton = findViewById(R.id.tv_edit);
            mDeleteButton = findViewById(R.id.iv_delete);

            // 编辑按钮
            mEditButton.setOnClickListener(v -> {
                if (mListener != null) {
                    int position = getViewHolderPosition();
                    if (position >= 0 && position < getCount()) {
                        mListener.onEdit(getItem(position), position);
                    }
                }
            });

            // 删除图标
            mDeleteButton.setOnClickListener(v -> {
                if (mListener != null) {
                    int position = getViewHolderPosition();
                    if (position >= 0 && position < getCount()) {
                        mListener.onDelete(position);
                    }
                }
            });
        }

        @Override
        public void onBindView(int position) {
            TerminalContactBean item = getItem(position);
            mNameView.setText(item.getName());
            mPhoneView.setText(item.getPhone());

            // 备注
            if (item.getNote() != null && !item.getNote().isEmpty()) {
                mNoteView.setVisibility(View.VISIBLE);
                mNoteView.setText(item.getNote());
            } else {
                mNoteView.setVisibility(View.GONE);
            }

            // 主联系人标签
            if (item.isPrimary()) {
                mPrimaryTag.setVisibility(View.VISIBLE);
            } else {
                mPrimaryTag.setVisibility(View.GONE);
            }
        }
    }

    /**
     * 新增按钮 ViewHolder
     */
    private final class AddViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mAddButton;

        private AddViewHolder() {
            super(R.layout.terminal_contact_add_item);
            mAddButton = findViewById(R.id.tv_add_contact);

            mAddButton.setOnClickListener(v -> {
                if (mListener != null) {
                    mListener.onAdd();
                }
            });
        }

        @Override
        public void onBindView(int position) {
            // 新增按钮不需要绑定数据
        }
    }

    /**
     * 联系人操作监听器
     */
    public interface OnContactActionListener {
        /** 新增联系人 */
        void onAdd();

        /** 编辑联系人 */
        void onEdit(TerminalContactBean contact, int position);

        /** 删除联系人 */
        void onDelete(int position);
    }
}
